import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;

/**
 * A simple language detector application using the Naive Bayes Classifier.
 */
public class SampleApp extends MIDlet implements CommandListener {
    private String language;
    private Display display;
    private TextBox txtBox;
    private Command btnCheck;
    private Classifier classifier;

    public SampleApp() {
        display = Display.getDisplay(this);
        txtBox = new TextBox("Language:", null, 250, 0);
        btnCheck = new Command("Check", Command.OK, 0);
        txtBox.addCommand(btnCheck);
        txtBox.setCommandListener(this);
        classifier = new Classifier();
        classifier.train("the and is if on in at a i you he she it we they", "English");
        classifier.train("le la les dans cette du un une de en je vous il elle et", "French");
        classifier.train("ein eine ich er sie der die das der wir und dieser", "German");
        classifier.train("a e o ou se no na eu um uma eu ele ela voce isso isto", "Portuguese");
        classifier.train("que ella y el las de la uno una en esta", "Spanish");
    }

    public void startApp() {
        display.setCurrent(txtBox);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command command, Displayable displayable) {
        if(command == btnCheck){
            language = classifier.classify(txtBox.getString());
            txtBox.setTitle("Language: " + language);
        }
    }
}
